package com.mischa.atc;

public class Heading {

	float _heading;
	
	public Heading() {
		_heading = 360;
	}
	
	public Heading(float heading) {
		set(heading);
	}

	public Heading(Heading copyFrom) {		
		this._heading = copyFrom._heading;
	}

	public float get() {
		return _heading;		
	}
	
	public double getRadians() {
		return Math.toRadians(_heading) % (2*Math.PI);
	}
	
	public void set(float newHeading)
	{
		_heading = convertToValidHeading(newHeading);
	}
	
	private static float convertToValidHeading(float heading) {
		float result = heading % 360;
		if(result <= 0f)
		{
			result += 360f;
		}		
		return result;
	}

	public void add(float amountToAdd) {
		set(_heading + amountToAdd);		
	}

	public boolean liesBetweenCW(float liesBetween, float andBetween) {
		float from = convertToValidHeading(liesBetween);
		float to = convertToValidHeading(andBetween);
		
		if(from < to) 
		{
			return from < _heading && _heading <= to;
		}
		else
		{		
			return (from < _heading && _heading <= 360.0) || (0 < _heading && _heading <= to);
		}
	}
	
	@Override
	public String toString() {
		return Utilities.padInteger((int)Math.rint(_heading), 3);
	}

	@Override
	public int hashCode() {
		return (int) _heading;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Heading other = (Heading) obj;
		if (this._heading != other._heading)
			return false;
		return true;
	}

	public boolean equals(float otherHeading) {
		return convertToValidHeading(otherHeading) == _heading;
	}
	
}
